﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web;

namespace Microsoft.Telco.WindowsLive
{


	public class AppInformation
	{

		// Methods
		internal AppInformation()
		{
			this.InitializeData(null, null, null, null, null);
		}

		public AppInformation(IDictionary<string, string> initParams)
		{
			if (initParams == null)
			{
				throw new ArgumentNullException("initParams");
			}
			Microsoft.Telco.WindowsLive.AppAuthentication authInfo = new Microsoft.Telco.WindowsLive.AppAuthentication();
			string clientId = null;
			foreach (string str2 in initParams.Keys)
			{
				string str = initParams[str2];
				if ((string.Compare(str2, "ClientID", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_clientId", StringComparison.OrdinalIgnoreCase) == 0))
				{
					clientId = str;
					continue;
				}
				if ((string.Compare(str2, "ClientState", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_clientState", StringComparison.OrdinalIgnoreCase) == 0))
				{
					this.ClientState = str;
					continue;
				}
				if ((string.Compare(str2, "AccessToken", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_accessToken", StringComparison.OrdinalIgnoreCase) == 0))
				{
					authInfo.AccessToken = HttpUtility.UrlDecode(str);
					continue;
				}
				if (string.Compare(str2, "RefreshToken", StringComparison.OrdinalIgnoreCase) == 0)
				{
					authInfo.RefreshToken = str;
					continue;
				}
				if ((string.Compare(str2, "Expiry", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_expiry", StringComparison.OrdinalIgnoreCase) == 0))
				{
					authInfo.Expiry = str;
					continue;
				}
				if ((string.Compare(str2, "UID", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_uid", StringComparison.OrdinalIgnoreCase) == 0))
				{
					authInfo.UserId = str;
					continue;
				}
				if ((string.Compare(str2, "Scope", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_scope", StringComparison.OrdinalIgnoreCase) == 0))
				{
					authInfo.GrantedOffers = Microsoft.Telco.WindowsLive.WrapProtocolHelper.ParseOffers(str);
					authInfo.Scope = str;
					continue;
				}
				if (((string.Compare(str2, "Error", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(str2, "c_error", StringComparison.OrdinalIgnoreCase) == 0)) && !string.IsNullOrEmpty(str))
				{
					authInfo.Error = new AuthorizationException(str);
				}
			}
			this.InitializeData(clientId, null, null, null, authInfo);
		}

		public AppInformation(string clientId, string clientSecret)
		{
			if (string.IsNullOrEmpty(clientId))
			{
				throw new ArgumentException("clientId");
			}
			if (string.IsNullOrEmpty(clientSecret))
			{
				throw new ArgumentException("clientSecret");
			}
			Collection<Microsoft.Telco.WindowsLive.Offer> requestedOffers = new Collection<Microsoft.Telco.WindowsLive.Offer>();
			requestedOffers.Add(new Microsoft.Telco.WindowsLive.Offer("WL_Profiles.View"));
			this.InitializeData(clientId, clientSecret, string.Empty, requestedOffers, null);
		}

        public AppInformation(string clientId, string clientSecret, Collection<Microsoft.Telco.WindowsLive.Offer> requestedOffers)
		{
			if (string.IsNullOrEmpty(clientId))
			{
				throw new ArgumentException("clientId");
			}
			if (string.IsNullOrEmpty(clientSecret))
			{
				throw new ArgumentException("clientSecret");
			}
			if ((requestedOffers == null) || (requestedOffers.Count == 0))
			{
				throw new ArgumentException("requestedOffers");
			}
			this.InitializeData(clientId, clientSecret, string.Empty, requestedOffers, null);
		}

        public AppInformation(string clientId, string clientSecret, Microsoft.Telco.WindowsLive.AppAuthentication authInfo)
		{
			if (string.IsNullOrEmpty(clientId))
			{
				throw new ArgumentException("clientId");
			}
			if (string.IsNullOrEmpty(clientSecret))
			{
				throw new ArgumentException("clientSecret");
			}
			if (authInfo == null)
			{
				throw new ArgumentNullException("authInfo");
			}
			Collection<Microsoft.Telco.WindowsLive.Offer> requestedOffers = new Collection<Microsoft.Telco.WindowsLive.Offer>();
			requestedOffers.Add(new Microsoft.Telco.WindowsLive.Offer("WL_Profiles.View"));
			this.InitializeData(clientId, clientSecret, string.Empty, requestedOffers, authInfo);

		}

        private void InitializeData(string clientId, string clientSecret, string callback, Collection<Microsoft.Telco.WindowsLive.Offer> requestedOffers, Microsoft.Telco.WindowsLive.AppAuthentication authInfo)
		{
			this.ServiceRoot = new Uri("http://apis.live.net");
			this.ConsentRoot = new Uri("https://consent.live.com/");
			this.ClientId = clientId;
			this.ClientSecret = clientSecret;
			this.CallbackUrl = callback;
			this.RequestedOffers = requestedOffers;
            this.AuthInfo = (authInfo != null) ? authInfo : new Microsoft.Telco.WindowsLive.AppAuthentication();
			this.PolicyUrl = string.Empty;
			this.TouUrl = string.Empty;
		}

		// Properties
        public Microsoft.Telco.WindowsLive.AppAuthentication AuthInfo { get; set; }
		public string CallbackUrl { get; set; }
		public string ClientId { get; set; }
		public string ClientSecret { get; set; }
		public string ClientState { get; set; }
		public Uri ConsentRoot { get; set; }

		internal Uri ConsentUrl
		{
			get
			{
				if (this.ConsentRoot != null)
				{
					return new Uri(this.ConsentRoot, "connect.aspx");
				}
				return null;
			}
		}

		public string PolicyUrl { get; set; }
		public Collection<Microsoft.Telco.WindowsLive.Offer> RequestedOffers { get; set; }
		public Uri ServiceRoot { get; set; }
		internal string SessionId { get; set; }
		public string TouUrl { get; set; }
	}

}